package com.dzt.androidframework.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.SplashActivity;

public class NotificationReceiver extends BroadcastReceiver {

	public final static String SHOW_ACTION = "com.dzt.androidframework.show";
	public final static String HIDE_ACTION = "com.dzt.androidframework.hide";
	public final static String HOME_ACTION = "com.dzt.androidframework.home";
	private String SYSTEM_REASON = "reason";
	private String SYSTEM_HOME_KEY = "homekey";
	private String SYSTEM_HOME_KEY_LONG = "recentapps";
	private NotificationManager notificationManager;

	public NotificationReceiver(){

	}

	public NotificationReceiver(Context context){
		notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	public IntentFilter getFilter() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(HIDE_ACTION);
		filter.addAction(HOME_ACTION);
		filter.addAction(SHOW_ACTION);
		filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
		return filter;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();
		if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
			String reason = intent.getStringExtra(SYSTEM_REASON);
			if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
				//表示按了home键,程序到了后台
				showNotification(context);
			} else if (TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)) {
				//表示长按home键,显示最近使用的程序列表
			}
		} else if (action.equals(HOME_ACTION)) {
			showNotification(context);
		} else if (action.equals(SHOW_ACTION)) {
			//stopForeground(true);
			notificationManager.cancelAll();
		} else if (action.equals(HIDE_ACTION)) {

		}
	}

	/**
	 * 显示Notification
	 *
	 * @param context
	 */
	private void showNotification(Context context) {
		CharSequence title = "App 通知";
		CharSequence text = "这是通知的文件信息，点击可以启动后台的App";
		Notification.Builder builder = new Notification.Builder(context);
		Intent hudIntent = new Intent(context, SplashActivity.class);
		hudIntent.setAction(Intent.ACTION_MAIN);
		hudIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		hudIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, hudIntent, 0);
		builder.setContentIntent(pendingIntent);
		builder.setSmallIcon(R.mipmap.ic_launcher);
		builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
				R.mipmap.ic_launcher));
		builder.setAutoCancel(true);
		builder.setContentTitle(title);
		builder.setContentText(text);
		Notification notification = builder.build();
		//notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification
		// .FLAG_AUTO_CANCEL;
		notificationManager.notify(0, notification);
	}
}
